"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FormDesignWidgetInfo = void 0;
exports.configToWidget = configToWidget;
exports.getWidgetConfig = void 0;
exports.getWidgetConfigCustomGroup = getWidgetConfigCustomGroup;
exports.getWidgetConfigTitle = getWidgetConfigTitle;
var _vue = require("vue");
var _core = require("@vxe-ui/core");
var _util = require("./util");
var _log = require("../../ui/src/log");
var _xeUtils = _interopRequireDefault(require("xe-utils"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// 控件原始配置信息，带响应
const refWidgetReactConfigMaps = (0, _vue.ref)({});
const getWidgetConfig = name => {
  const widgetReactConfigMaps = refWidgetReactConfigMaps.value;
  return widgetReactConfigMaps[name] || {};
};
exports.getWidgetConfig = getWidgetConfig;
function getWidgetConfigTitle(name, $xeFormDesign) {
  const widgetConf = getWidgetConfig(name);
  if (widgetConf) {
    const configTitle = widgetConf.title;
    const params = {
      name,
      $formDesign: $xeFormDesign
    };
    return _xeUtils.default.toValueString(_xeUtils.default.isFunction(configTitle) ? configTitle(params) : configTitle);
  }
  return name;
}
function getWidgetConfigCustomGroup(name, $xeFormDesign) {
  const widgetConf = getWidgetConfig(name);
  if (widgetConf) {
    const configCustomGroup = widgetConf.customGroup;
    const params = {
      name,
      $formDesign: $xeFormDesign
    };
    return _xeUtils.default.toValueString(_xeUtils.default.isFunction(configCustomGroup) ? configCustomGroup(params) : configCustomGroup);
  }
  return name;
}
const validWidgetUniqueField = (field, widgetObjList) => {
  return !_xeUtils.default.findTree(widgetObjList, item => item.field === field, {
    children: 'children'
  });
};
class FormDesignWidgetInfo {
  constructor($xeFormDesign, name, widgetObjList) {
    Object.defineProperty(this, "id", {
      enumerable: true,
      configurable: true,
      writable: true,
      value: 0
    });
    Object.defineProperty(this, "field", {
      enumerable: true,
      configurable: true,
      writable: true,
      value: ''
    });
    Object.defineProperty(this, "title", {
      enumerable: true,
      configurable: true,
      writable: true,
      value: ''
    });
    Object.defineProperty(this, "name", {
      enumerable: true,
      configurable: true,
      writable: true,
      value: ''
    });
    Object.defineProperty(this, "required", {
      enumerable: true,
      configurable: true,
      writable: true,
      value: false
    });
    Object.defineProperty(this, "hidden", {
      enumerable: true,
      configurable: true,
      writable: true,
      value: false
    });
    Object.defineProperty(this, "options", {
      enumerable: true,
      configurable: true,
      writable: true,
      value: {}
    });
    Object.defineProperty(this, "children", {
      enumerable: true,
      configurable: true,
      writable: true,
      value: []
    });
    Object.defineProperty(this, "model", {
      enumerable: true,
      configurable: true,
      writable: true,
      value: {
        update: false,
        value: ''
      }
    });
    let customField = '';
    if (name) {
      const compConf = _core.renderer.get(name) || {};
      if (compConf) {
        const widgetReactConfigMaps = refWidgetReactConfigMaps.value;
        const createWidgetFormConfig = compConf.createFormDesignWidgetConfig;
        if (createWidgetFormConfig) {
          const params = {
            name,
            $formDesign: $xeFormDesign
          };
          const widgetConfig = createWidgetFormConfig(params) || {};
          const titleConf = widgetConfig.title;
          const fieldConf = widgetConfig.field;
          this.title = _xeUtils.default.toValueString(_xeUtils.default.isFunction(titleConf) ? titleConf(params) : titleConf);
          this.options = widgetConfig.options || {};
          this.children = widgetConfig.children || [];
          if (fieldConf) {
            if (_xeUtils.default.isFunction(fieldConf)) {
              customField = fieldConf({
                name,
                $formDesign: $xeFormDesign
              });
            } else {
              customField = fieldConf;
            }
          }
          if (!widgetReactConfigMaps[name]) {
            widgetReactConfigMaps[name] = Object.assign({}, widgetConfig);
            refWidgetReactConfigMaps.value = Object.assign({}, widgetReactConfigMaps);
          }
        }
      }
    }
    const widgetId = (0, _util.getNewWidgetId)(widgetObjList);
    if (customField) {
      // 如果使用了自定义字段，验证字段名是否唯一
      if (!validWidgetUniqueField(customField, widgetObjList)) {
        (0, _log.errLog)('vxe.error.uniField', [customField]);
      }
    }
    this.id = widgetId;
    this.field = customField || `${name}${widgetId}`;
    this.name = name;
  }
}
exports.FormDesignWidgetInfo = FormDesignWidgetInfo;
function configToWidget(conf) {
  const widget = new FormDesignWidgetInfo(null, conf.name, []);
  widget.id = conf.id;
  widget.title = conf.title || '';
  widget.field = conf.field || '';
  widget.required = conf.required || false;
  widget.hidden = conf.hidden || false;
  widget.options = Object.assign({}, widget.options, conf.options);
  widget.children = conf.children ? conf.children.map(item => configToWidget(item)) : [];
  return widget;
}